package com.sxbbc.agent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.agent.service.AgentBaseService;
import com.sxbbc.agent.util.AgentSession;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.encrypts.des.DESEncode;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.AgAgent;
import com.sxbbc.common.core.entity.jz.AgMenus;
import com.sxbbc.common.core.entity.jz.AgRoles;
import com.sxbbc.common.core.service.IAgAgentService;
import com.sxbbc.common.core.service.IAgMenusService;
import com.sxbbc.common.core.service.IAgRolesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 基础服务 实现类
 *
 * @author leehao
 * @date 2019-07-10
 */
@Service
public class AgentBaseServiceImpl implements AgentBaseService {

    @Autowired
    private IAgAgentService iAgAgentService;
    @Autowired
    private IAgRolesService iAgRolesService;
    @Autowired
    private IAgMenusService iAgMenusService;

    @Override
    public RestResponse doLogin(String login, String passWord, HttpServletRequest request) {
        CalcuUtils calUtil = CalcuUtils.getInstance();
        AgAgent agAgent = iAgAgentService.selectByLogin(login);
        if (agAgent == null) {
            return GetRest.getFail("该用户不存在");
        }
        if (!calUtil.isEquals(agAgent.getStatus(), StaticUtils.STATUS_SUCCESS)) {
            return GetRest.getFail("该用户已过期");
        }
        //TODO 部署上线需要取消注释
        String encryptPassword = DESEncode.getInstance().encrypt(passWord);
        if (!StringUtils.equals(agAgent.getPassWord(),encryptPassword)) {
            return GetRest.getFail("密码错误");
        }
        AgRoles agRole = iAgRolesService.getById(agAgent.getRoleId());
        if (agRole == null) {
            return GetRest.getFail("用户所属角色不存在");
        }
        List<Integer> list = null;
        try {
            list = calUtil.getIntegerList(agRole.getMenuIds().split(","));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (CollectionUtils.isEmpty(list)) {
            return GetRest.getFail("当前角色没有菜单权限");
        }
        QueryWrapper<AgMenus> wrapper = new QueryWrapper<>();
        wrapper.in("id",list);
        wrapper.eq("status",StaticUtils.STATUS_YES);
        wrapper.orderByAsc("sort");
        //代理隐藏部分信息
        agAgent.setPassWord(null);
        agAgent.setPayWord(null);
        List<AgMenus> menuList = iAgMenusService.list(wrapper);
        AgentSession.setMenu(request,menuList);
        AgentSession.setMenuUrl(request,menuList.stream().map(AgMenus :: getUrl).collect(Collectors.toList()));
        AgentSession.setAgent(request,agAgent);
        return GetRest.getSuccess("登录成功");
    }
}
